package com.simplemall.micro.serv.tel.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.simplemall.micro.serv.common.bean.telbook.TelBook;
import com.simplemall.micro.serv.common.constant.SystemConstants;
import com.simplemall.micro.serv.tel.service.TelBookService;

@RestController
@RequestMapping("/telBook")
@RefreshScope
public class TelBookController {

	@Autowired
	TelBookService telBookService;
	
	@Value("${user.telBook.count}")
	private int telBookCount;
	
	/**
	 * 获取电话薄组的信息
	 * 
	 * @param prdId
	 * @return
	 */
	@RequestMapping(value = "telBookList/{userid}", method = RequestMethod.POST)
	public List<TelBook> getTelBookById(@PathVariable("userid") String userid) {
		return telBookService.getTelBookByUserid(userid);
	}

	/**
	 * 创建电话薄组
	 * @param bookName
	 * @param remark
	 * @param userid
	 */
	@RequestMapping(value = "telBookCreate", method = RequestMethod.POST)
	public String createTelBook(@RequestParam("bookName") String bookName, @RequestParam("remark") String remark, @RequestParam("userid") String userid){
		int telBookSize = telBookService.getTelBookCountByUserid(userid);
		if(telBookSize >= telBookCount){
			//超出创建数目
			return SystemConstants.TEL_BOOK.UPPERLIMIT;
		}
		TelBook telBook = new TelBook();
		telBook.setBookName(bookName);
		telBook.setRemark(remark);
		telBook.setUserid(userid);
		int result = telBookService.createTelBook(telBook);
		return result > 0 ? SystemConstants.Code.SUCCESS : SystemConstants.Code.FAIL;
	}
}
